package Graphique;

import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Sash;

import Connexion.ConnectionFactory;
import Listener.ListListener;

public class CompositeList extends Composite {

	private FormData listData;
	private Sash sash;
	private ExpandedSearch expandSearch;
	private List listFilms;
	private FillLayout fl;
	private MainFrame main;
	private ConnectionFactory connection;

	public CompositeList(Composite shell, int style) {
		super(shell, style);

		fl = new FillLayout();
		fl.type = SWT.VERTICAL;
		fl.spacing = 10;
		this.setLayout(fl);
	}

	private void createsFormData() {

		listData = new FormData();
		listData.left = new FormAttachment(0, 0);
		listData.right = new FormAttachment(sash, 0);
		listData.top = new FormAttachment(0, 0);
		listData.bottom = new FormAttachment(100, 0);

		this.setLayoutData(listData);

	}

	public void initExpandSearch() {
		expandSearch = new ExpandedSearch(this, SWT.NONE);
	}

	public void initList(ConnectionFactory connection) {
		if (this.connection == null) {
			this.connection = connection;
		}
		if (listFilms == null) {
			listFilms = new List(this, SWT.V_SCROLL);
		}

		this.connection.executeQuery(1, null, -1, -1);
		try {
			while (this.connection.getResults().next()) {
				listFilms.add(this.connection.getResults()
						.getString("nameFilm"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void search(ConnectionFactory connection, String name, int year,
			int genre) {
		if (this.connection == null) {
			this.connection = connection;
		}

		// listFilms = new List(this, SWT.V_SCROLL);

		this.connection.executeQuery(2, name, year, genre);
		try {
			while (this.connection.getResults().next()) {
				listFilms.add(this.connection.getResults()
						.getString("nameFilm"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void setSash(Sash sash) {
		this.sash = sash;
		createsFormData();

	}

	public void setMainFrame(MainFrame main) {
		this.main = main;
		listFilms.addListener(SWT.Selection, new ListListener(listFilms,
				this.main));
	}

	public List getList() {
		return this.listFilms;
	}

}
